package com.shenyong.ladmin.service.Impl;

import com.shenyong.entity.LoginUsers;
import com.shenyong.exception.MyException;
import com.shenyong.ladmin.mapper.MenuMapper;
import com.shenyong.ladmin.mapper.UsersMapper;
import com.shenyong.ladmin.entity.*;
import com.shenyong.ladmin.result.Page;
import com.shenyong.ladmin.service.UsersService;
import com.shenyong.mapper.UserMapper;
import com.shenyong.util.JwtUtil;
import com.shenyong.util.MD5Untils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.LockedAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create By:qiang
 * Date: 2019/11/25 20:28
 * 文件说明: UsersServiceImpl
 */
@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UsersMapper usersMapper;
    @Override
    public List<Users> getUsersPage(Page page) {
        List<Users> listuser = usersMapper.getUsersPage(page);
        return listuser;
    }

    @Override
    public int getUsersCount(Page page) {
        return usersMapper.getUsersCount(page);
    }

    @Override
    public int updateUser(Users user) {
        return usersMapper.updateUser(user);
    }

    @Override
    public List<Level> userLevelAll() {
        return usersMapper.userLevelAll();
    }

    @Override
    public int updateUserStateByUser_Id(Users user) {
        if(user.getUser_enabled().equals("0")){
            user.setUser_enabled("1");
        }else {
            user.setUser_enabled("0");
        }
        return usersMapper.updateUserStateByUser_Id(user);
    }
    public LoginUsers usersLogin(Users users) {
com.shenyong.entity.Users user1 = null;
        user1=userMapper.getUserByName(users.getUser_username());
        if (user1 == null) {
            throw new MyException("该用户名不存在,请检查后再登录!(๑¯◡¯๑)");
        }
        if (!MD5Untils.verify(users.getUser_password(),user1.getUser_password())) {
            throw new AccountException("密码错误，请仔细一点哦(`･ω･′)ゞ");
        }
        if (user1.getUser_enabled() == 1){
            throw new LockedAccountException("用户被锁定,请联系管理员!๐·°(৹˃̵﹏˂̵৹)°·๐");
        }
        //Map<String,Object> map = new HashMap<>();
        // List<String> rolePre = userMapper.selectRoleDetails(user1.getUser_id());
        //map.put("prems",rolePre);
        LoginUsers loginUser=new LoginUsers();
        //根据电话号码和密码加密生成token
        String token = JwtUtil.sign(user1.getUser_username(), user1.getUser_password());
        loginUser.setToken(token);
        //loginUser.setPrems(rolePre);
        loginUser.setUser(user1);
        return loginUser;
    }

    public int userAdd(Users users) {
        return usersMapper.userAdd(users);
    }

    public int savePassword(Map<String,Object> map) {
        return usersMapper.savePassword(map);
    }

    @Override
    public Map<String,Object> getAdminUser(Page page) {
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code",0);
        map.put("msg","ok");
        map.put("data",usersMapper.getAdminUser(page));
        map.put("count",usersMapper.getAdminUserCount(page));
        return map;
    }

    @Override
    public int getAdminUserCount(Page page) {
        return usersMapper.getAdminUserCount(page);
    }

    @Override
    public int saveAdminUser(Users users) {
        com.shenyong.entity.Users user1= userMapper.getUserByName(users.getUser_username());
        if(user1!=null){
            throw new MyException("此用户名已存在");
        }
        String password= MD5Untils.generate("admin");
        System.out.println("是否是同一字符串："+MD5Untils.verify("admin",password));
        users.setUser_password(password);
        users.setUser_enabled("0");
        users.setUser_role(1);
        return usersMapper.saveAdminUser(users);
    }

    @Override
    public List<PrivilegeRes> getPrivilegeHierarchy() {
        List<PrivilegeRes> list = new ArrayList<>();
        List<PrivilegeRes> childPrivilegeResList;
        List<PrivilegeRes> childrenPrivilegeResList;
        List<Menu> privilegeList = menuMapper.getByParent(0);
        List<Menu> childPrivilegeList;
        List<Menu> childrenPrivilegeList;
        PrivilegeRes privilegeRes;
        PrivilegeRes childPrivilegeRes;
        PrivilegeRes childrenPrivilegeRes;
        for(Menu obj : privilegeList){
            childPrivilegeResList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(privilegeList)){
                childPrivilegeRes = new PrivilegeRes();
                childPrivilegeRes.setId(obj.getMenu_id());
                childPrivilegeRes.setLabel(obj.getMenu_name());
                list.add(childPrivilegeRes);
                childPrivilegeList = menuMapper.getByParent(obj.getMenu_id());
                if(CollectionUtils.isNotEmpty(childPrivilegeList)){
                    childrenPrivilegeResList = new ArrayList<>();
                    for(Menu obj1:childPrivilegeList){
                        childrenPrivilegeRes = new PrivilegeRes();
                        childrenPrivilegeRes.setId(obj1.getMenu_id());
                        childrenPrivilegeRes.setLabel(obj1.getMenu_name());
                        childrenPrivilegeResList.add(childrenPrivilegeRes);
                        if(CollectionUtils.isEmpty(childPrivilegeList)){
                            continue;
                        }
                    }
                    childPrivilegeRes.setChildren(childrenPrivilegeResList);
                    childPrivilegeResList.add(childPrivilegeRes);
                }
            }
        }
        return list;
    }

    public List<Users> getFriends(Users users){
        return usersMapper.getFriends(users);
    }

    public Note randNote(){
        return usersMapper.randNote();
    }

    public List<Article> getCollections(Collection collection){
        return usersMapper.getCollections(collection);
    }


}
